package com.moyun.syssecurity.sms.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.moyun.syssecurity.sms.service.SendSms;
import com.moyun.syssecurity.sms.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SendSmsImpl implements SendSms {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public boolean addSendSms(String PhoneNumbers) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = SmsUtil.createClient("LTAI5tEAKEL6SLasfH1e6K6Q", "lqoB5Wp0H514RB1rdnK2QZJCE2P73t");
        String code = RandomUtil.randomNumbers(6);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(PhoneNumbers)
                .setSignName("传智健康")
                .setTemplateCode("SMS_195724923")
                .setTemplateParam("{\"code\":\""+ code +"\"}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        boolean flag = sendSmsResponse.body.code.equals("OK");
        // 将短信验证码存入到redis缓存中
        this.redisTemplate.opsForValue().set(PhoneNumbers,code,5*60, TimeUnit.SECONDS);

        return flag;
    }
}